package org.jfk.common.ctrl;

import java.util.List;

import org.apache.log4j.Logger;
import org.jfk.core.controller.BaseController;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;

public class SuggestController extends BaseController {
	private static Logger logger = Logger.getLogger(SuggestController.class);
	public void index(){
		String searchClass = getPara("searchClass");
		String searchField = getPara("searchField");
		String searchValue = getPara("searchValue");
		Table table;
		try {
			table = TableMapping.me().getTable((Class<Model>)Class.forName(searchClass));
			String tableName = table.getName();
			String sql = "select id, " + searchField + " from "+ tableName + " where " + searchField + " like '%" + searchValue + "%' limit 20";
			List<Record> list = Db.find(sql);
			setAttr("searchResult",list);
			super.renderJson(list);
		} catch (ClassNotFoundException e) {
			logger.error(e.getMessage(),e);
			super.renderError(505);
		}
	}
}
